#include <sys/types.h>
#include <pwd.h>
#include <stdio.h>
#include <unistd.h>
#include <grp.h>
#include <string.h>

int createsetup()
{
	struct passwd *p;
	char *path;
	path = "settings.txt";
	FILE *file = fopen(path, "w");

	if((p = getpwuid(getuid())) == NULL)
		perror("getpwuid() error");
	else {
		
		p->pw_name[strlen(p->pw_name)] = 0;
		fprintf(file,"%s\n", p->pw_name);
		fprintf(file, "%s", "log.txt");
		
	}

	fclose(file);
	return 0;

}

//Type is type of change we want to make (/log which changes logpath or /nick which changes nickname)
int changesetup(char *type, char *value)
{
	struct passwd *p;
	char *path;
	char line_buffer[BUFSIZ];
	path = "settings.txt";
	FILE *f;		

	if(strcmp(type, "/setlogpath") == 0){
		
		f = fopen(path, "r");
		fgets(line_buffer, sizeof(line_buffer), f);
		fclose(f);

		f = fopen(path, "w");
		fprintf(f,"%s", line_buffer);
		fprintf(f, "%s", value);
		fclose(f);

	}

	else if(strcmp(type, "/setnick") == 0){
		int i = 0;
		f = fopen(path, "r");

		while(i < 2){
			fgets(line_buffer, sizeof(line_buffer), f);
			i++;
		}

		fclose(f);

		f = fopen(path, "w");
		fprintf(f,"%s\n", value);
		fprintf(f, "%s\n", line_buffer);
		fclose(f);
	}

	return 0;
}